--[[
 -- *名称:cards
 -- *描述:牌的列表
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-07
 -- *修改日期:2017-11-07
 -- @return [description]
 ]]

local Card = require(basePath .. "domain/card");
local Cards = pokercore.class();

--设置元表
local mt = {};

---
 -- [__eq 相等]
 -- @DateTime 2017-11-07
 -- @param    c1 [description]
 -- @param    c2 [description]
 -- @return    [description]
 --
mt.__eq = function (c1, c2) 
   if not c2 or not c1 then
      return false ;
   elseif c1 == c2 then
      return true ;
   end 
   -- 比较cardList是否相等,要重写。。 cardList数量必须相等
   for k,v in ipairs(c1.cardList or {}) do
       if v ~= c2.cardList[k] then
          return false ;
       end      
   end
   return true;
end

mt.__tostring = function (t)
   local cardStr = "" ;
   if not t.cardList then
     cardStr = "nil" ;
   end
   if t.cardList and #t.cardList == 0 then
     cardStr = "空" ;
   end
   for k,card in ipairs(t.cardList) do
      cardStr = cardStr .. "("..tostring(card)..")"..","
   end
   return "Cards [cardList=" .. cardStr .. ", result=" .."result ->"..(t.result and tostring(t.result) or "nil") .. "]";
end

function Cards:ctor(cards)
   --结果集
   self.result = nil ; 
   self.cardList = {} ;
   table.addAll(self.cardList,cards or {});
   -- for _,card in ipairs(cards) do
   --    if pokercore.typeof(card) == type(Card) then
   --        table.insert(self.cardList,card);
   --    else
   --        table.insert(self.cardList,pokercore.new(Card,CardShape.DIAMOND,card,card));
   --    end
   -- end


   local meta = getmetatable(self) or {};
   table.merge(meta,mt)
   setmetatable(self,meta)
end


function Cards:addCard(card)
    if card then
       table.insert(self.cardList,card);
    end
end

function Cards:addCards(cards)
    if cards then
       table.addAll(self.cardList,cards);
    end
end

function Cards:get(index)
    return self.cardList[index] ;
end

function Cards:size()
    return #self.cardList ;
end
---
 -- [compareTo 和其他Cards比较]
 -- @DateTime 2017-11-07
 -- @param    o [description]
 -- @return    [description]
 --
function Cards:compareTo(o)
   	if (o.result == null) then
		return 1;
	elseif (self.result == null) then
		return -1;
	else 
		return self.result:compareTo(o.result);
	end
end

function Cards:clone()
    return pokercore.clone(self);
end

---
 -- [containsAll 检测是否包含所有牌]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @return    [description]
 --
function Cards:containsAll(cards)
   if not cards then
   	   return false ;
   end 
   for k,v in pairs(cards) do
   	  if self.cardList[k] ~= v then
         return false ;
   	  end
   end
   return true ;
end

---
 -- [removeAll 移除所有cards]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @return    [description]
 --
function Cards:removeAll(cards)
   for k,v in ipairs(cards or {}) do
       for k1,v1 in ipairs(self.cardList) do
 			if v1 == v then
 			   table.remove(self.cardList,k1);
 			   break ;
 			end
       end
   end
end

---
 -- [subList 截取子序列,注意fromIndex从1开始]
 -- @DateTime 2017-11-07
 -- @param    fromIndex [description]
 -- @param    toIndex [description]
 -- @return    [description]
 --
function Cards:subList(fromIndex,toIndex)
   local subList = {} ;
   for i=fromIndex,toIndex do
      table.insert(subList,self.cardList[i]);
   end
   return subList ;
end

function Cards:clear()
   self.cardList = {} ;
end

function Cards:getType()
   return self.result:getType();
end

function Cards:getCardList()
   return self.cardList ;
end

function Cards:setCardList(cardList)
   self.cardList = cardList ;
end

function Cards:getResult()
   return self.result ;
end

function Cards:setResult(result)
   self.result = result
end





return Cards ;